var wsObj=null;
var wsUri=null;
var userId=-1;

function createWebSocket(){
    var host=window.location.host;
    userId=GetQueryString("userId");
    wsUri="ws://"+host+"/websocket/"+userId;
    console.log(userId)
    try{
        wsObj=new WebSocket(wsUri);
        initWsEventHandle();
    }catch(e){
        writeToScreen("执行关闭事件,开始重连");
    }

}

function initWsEventHandle(){
    try{
        wsObj.onopen=function (evt){
            onWsOpen(evt);
        };

        wsObj.onmessage=function (evt){
          onWsMessage(evt);
        };

        wsObj.onclose=function (evt){
            writeToScreen("执行关闭事件,开始重连");
            onWsClose(evt);
        };

        wsObj.onerror=function (evt){
            writeToScreen("执行error关闭事件,开始重连");
            onWsError(evt);
        };

    }catch (e){
        writeToScreen("绑定事件没有成功");
    }

}

function onWsOpen(evt){
    writeToScreen("已建立连接");
}

function onWsClose(evt){
    writeToScreen("已断开连接");
}

function onWsError(evt){
    writeToScreen(evt.data);
}

function writeToScreen(message){
    if(DEBUG_FLAG){
        $("#debuggerInfo").val($("#debuggerInfo").val+"\n"+message);
    }
}

function GetQueryString(name){
    //用正则表达式获取url中每个参数
    var reg= new RegExp("(^|&)"+ name + "=([^&]*)(&|$)","i");
    //window.location 对象用于获得当前页面的地址 (URL)
    var r = window.location.search.substr(1).match(reg);
    var context="";
    if(r!=null)
        context=r[2];
    reg=null;
    r=null;
    return context==null || context=="" || context=="undefined" ? "" : context;
}